/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package horde.internalResources.editor;

import horde.internalResources.dictionaries.dictionary;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.EventObject;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Administrador
 */

public class FSMStatePanel extends JPanel implements TableCellRenderer, TableCellEditor {

    private JLabel nameLabel;
    private JTextField nameField;
    private JTable actionsTable;
    private DefaultTableModel actionsTableModel;
    private JScrollPane scroll;

    public FSMStatePanel () {
        super();
        init();
    }

    public FSMStatePanel (Boolean demo) {
        super();
        init();
        if (demo) {
            Object [] data = {"doWork", "workToDo", "workDone", "Working Error"};
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
            actionsTableModel.addRow(data);
        }
    }

    private void init() {
        setLayout(new BorderLayout());
        nameLabel =new JLabel(dictionary.getString("words.name"));
        nameField = new JTextField("");
        nameField.setPreferredSize(new Dimension(200,30));
        actionsTable = new JTable();
//        {
//            @Override public boolean isCellEditable (int row, int column) {
//                return true;
//            }
//        };
        actionsTableModel = new DefaultTableModel();
        scroll = new JScrollPane(actionsTable);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        actionsTableModel.addColumn("Request");
        actionsTableModel.addColumn("Run");
        actionsTableModel.addColumn("On sucess");
        actionsTableModel.addColumn("On error");
        actionsTable.setModel(actionsTableModel);
        JPanel panelName = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panelName.add (nameLabel);
        panelName.add (nameField);
        /* ..... */
        setBorder(BorderFactory.createLineBorder(Color.black));
        add (panelName, BorderLayout.PAGE_START);
        add (scroll, BorderLayout.CENTER);
    }

    // <editor-fold defaultstate="collapsed" desc="Cell Renderer">
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        FSMStatePanel p = (FSMStatePanel) value;
        p.setSelected(isSelected);
        p.setEnabled(true);
        return p;
    }

    public void setSelected (boolean isSelected) {
        if (isSelected) setBorder(BorderFactory.createLineBorder(Color.blue, 3));
        else this.setBorder(null);
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Cell editor">
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        FSMStatePanel p = (FSMStatePanel) value;
        return p;
    }

    public Object getCellEditorValue() {
        return null;
    }

    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    public boolean stopCellEditing() { return true; }

    public void cancelCellEditing() {}

    public void addCellEditorListener(CellEditorListener l) {}

    public void removeCellEditorListener(CellEditorListener l) {}
    // </editor-fold>
}
